package com.example.day_blog.config;

import com.example.day_blog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author starsea
 * @date 2024-01-19 14:46
 */
@Slf4j
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //进行用户拦截
    //    if(HttpMethod.OPTIONS.toString().equals(request.getMethod()))
    //    {
    //        return true;
    //    }
        //1.从header中获取token
        String token=request.getHeader("user_token");
        log.info("从Header中获取token1   "+token);
        Claims claims= JwtUtils.parseToken(token);
        //验证token
        if(claims==null)
        {
            //拦截
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
